﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hutech.Authentication.Library.Web.Framework;
using Resources;
using Hutech.Authentication.Library.Business.WebHelpers;
using log4net;
using Hutech.Authentication.Library.Business;
using System.Text;

namespace Hutech.CRM.Web.Admin
{
    public partial class RoleAndPermission : hutechBasePage
    {
        #region Variable
        //private Role role;
        //protected string DeleteLinkImage = "~/Data/SiteImages/" + WebConfigSettings.DeleteLinkImage;
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!WebUser.IsAdminOrRoleAdmin)
            {
                SiteUtils.RedirectToAccessDeniedPage(this);
                return;
            }
            SecurityHelper.DisableBrowserCache();
            PopulateControls();
            PopulateLabels();
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            LinkButtonUpdate.Click += new EventHandler(LinkButtonUpdate_Click);
        }


        #region Load All data
        private void PopulateControls()
        {
            if (Page.IsPostBack)
                return;
            LoadParams();
            BindModuleList();
        }
        private void PopulateLabels()
        {
            Role item = new Role(new Guid(HiddenFieldRoleID.Value));
            Title = Resource.AdminMenuRoleAdminLink;
            spnTitle.InnerText = item.RoleName;//Resource.SecurityRolesTitle;

            lnkAdminMenu.Text = Resource.AdminMenuLink;
            lnkAdminMenu.ToolTip = Resource.AdminMenuLink;
            //lnkAdminMenu.NavigateUrl = SiteRoot + "/Admin/AdminMenu.aspx";
            lnkRoleManager.Text = Resource.AdminMenuRoleAdminLink;
            lnkRoleManager.ToolTip = Resource.AdminMenuRoleAdminLink;
            //lnkRoleManager.NavigateUrl = SiteRoot + "/Admin/RoleManager.aspx";

        }

        private void BindModuleList()
        {
            List<RolePermission> item = RolePermission.GetAllByRoleID(new Guid(HiddenFieldRoleID.Value));
            List<Actions> listAction = Actions.GetAll();
            int countItem = item.Count;
            int countAction=listAction.Count;

            //Kiem tra isSelect
            for (int i = 0; i < countItem; i++)
            {
                for (int j = 0; j < countAction; j++)
                {
                    if (item[i].ActionGuid == listAction[j].ActionGuid)
                    {
                        listAction[j].IsSelect = true;
                        break;
                    }
                }
            }

            DataListAction.DataSource = listAction;
            DataListAction.DataBind();
        }
        private void LoadParams()
        {
            HiddenFieldRoleID.Value = WebUtils.ParseGuidFromQueryString("roleid", Guid.Empty).ToString();
        }
        #endregion

        #region All Event
        void LinkButtonUpdate_Click(object sender, EventArgs e)
        {
            int count = DataListAction.Items.Count;
            bool flag = true;
            for (int i = 0; i < count; i++)
            {
                DataListItem item = DataListAction.Items[i];
                CheckBox CheckBoxSelect = (CheckBox)item.FindControl("CheckBoxSelect");
                HiddenField HiddenFieldActionGuid = (HiddenField)item.FindControl("HiddenFieldActionGuid");
                RolePermission rolePermission = new RolePermission(new Guid(HiddenFieldRoleID.Value.ToString()), new Guid(HiddenFieldActionGuid.Value.ToString()));
                if (CheckBoxSelect.Checked)
                {
                    rolePermission.ActionGuid = new Guid(HiddenFieldActionGuid.Value.ToString());
                    rolePermission.RoleId = new Guid(HiddenFieldRoleID.Value.ToString());
                    rolePermission.EditDate = DateTime.Now;
                    rolePermission.EditUserID = WebUser.GetCurrentUserId();
                    if (!rolePermission.Save())
                        flag = false;
                }
                else
                    if (!RolePermission.Delete(rolePermission.RolePermissionGuid))
                        flag = false;
            }

            if (!(Page.IsStartupScriptRegistered("AlertScript") && flag))
            {
                Page.RegisterStartupScript("AlertScript", "<script type='text/javascript'>alert('Cập nhật thành công.');</script>");
                //Response.Redirect("RoleManager.aspx");
            }
            else
                Page.RegisterStartupScript("AlertScript", "<script type='text/javascript'>alert('Cập nhật thất bại.');</script>");
            
        }
        #endregion
    }
}